﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueryData;

public class 分组查询
{
    void MyMethod()
    {
        using (var db = new BasicDbContext())
        {
            // 在分组连接中对分组进行聚合后 ，EF Core会翻译为关系数据库的分组查询
            (from x in db.BasicEntities
             group x by x.Text into grouping
             select new
             {
                 Text = grouping.Key,
                 Count = grouping.Count()
             }).ToList();

            // 分组连接的where操作会翻译为分组查询的having子句
            (from x in db.BasicEntities
             group x by new { x.Text, x.Number } into grouping
             where grouping.Count() > 1
             orderby grouping.Count() descending
             select new
             {
                 Text = grouping.Key.Text,
                 Count = grouping.Count()
             }).ToList();

        }

    }
}
